<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
session_start();
include_once("../../config.inc.php");

$_LangPath = "account_bet";
$strTemplatePath = __Admintemplate_Path."/AccountNew/";
include_once(__Language_Path."/selectlanguage_admin.php");
include_once(__Common_Path."/ShowScore_1.inc.php");

if($_NumberNormal != "" || $_Message != ""){
	$GLOBALS["errormessage"] = $_strAccount_Bet["onCountResult"];//"結帳中，請稍後！！";
	ErrMsgOutputHTML("Error", "", 1);
}

$arrPower = CheckUsePower("account");
if($arrPower === false)ErrMsgOutputHTML("Error", "", 1);

include_once(__Admin_Path."/logincheck_admin_listdata.php");
include_once(__Common_Path."/Lotto.inc.php");
include_once(__Modules_Path."/Lotto.lib.php");
include_once(__Modules_Path."/ChangeTime.lib.php");

$intGID = (int)$_data["GID"];
$isRealShow = (int)$_data["RealShow"];

$fp = @file($strTemplatePath."WagerItemSort.html");
$fp = implode("", $fp);
$fp = setHTMLCharset($fp);


$intUserID = $_SESSION["Admin_Info"] -> ID;
$strUserRate = "";
switch($arrPower[1]){
	case "agent-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "agent":						//顯示該代理商全部及時注單
		$strSeach = " and b.SubID = ".$intUserID." ";
		if($isRealShow == 1)$strUserRate = "*IF(b.WagerType = 2,-1,1 - b.SubRate)";
		break;

	case "main-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "main":					//顯示該總代理全部及時注單
		$strSeach = " and b.MainID = ".$intUserID." ";
		if($isRealShow == 1)$strUserRate = "*IF(b.WagerType = 3,-1,b.SubRate - b.MainRate)";
		break;

	case "stockholder-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "stockholder":					//顯示該股東全部及時注單
		$strSeach = " and b.StockID = ".$intUserID." ";
		if($isRealShow == 1)$strUserRate = "*IF(b.WagerType = 4,-1,b.MainRate - b.StockRate)";
		break;

	case "director-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "director":				//顯示該總監全部及時注單
	case "director-noitem":
		$strSeach = " and b.DirectorID = ".$intUserID." ";
		if($isRealShow == 1)$strUserRate = "*IF(b.WagerType = 5,-1,b.StockRate - b.DirectorRate)";
		break;

	case "all":
	case "all-limit":
		if($isRealShow == 1)$strUserRate = "*ROUND(b.DirectorRate, 4)";
		break;

	default:
		$title = "Error";
		$GLOBALS["errormessage"] =  $_strAccount_Bet["NoPower"]; //""很抱歉！您沒有此程式的使用權限！";
		ErrMsgOutputHTML("Error", "", 1);
		break;
}
$arrRows = array();
//六肖
$strSQL = "select b.Bet_Type, a.JoinNumber as JoinNumber, b.Number as Number, SUM(b.MemStake*ROUND(b.MemExchangeRate, 4){UserRate}) as MemStake, SUM(b.MemPayOff*ROUND(b.MemExchangeRate, 4){UserRate}) as MemPayOff, COUNT(a.ID) as WagerCount from `bet_BetListContent` a, `bet_BetListTmp` b where b.GambleID = ".$intGID." and b.isPayOff = 0 and b.Bet_Type = 30 and a.ID = b.ID ".$strSeach." group by a.JoinNumber, b.Number order by MemPayOff desc limit 0, 20 ";
$strSQL = str_replace("{UserRate}", $strUserRate, $strSQL);
if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errorhidden"] = "Find Data Error!";
	$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".mysql_error($$_Conn).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
while($objR = PMA_mysql_fetch_object($RS)){
	$arrRows[$objR -> Bet_Type][$objR -> Number][] = $objR;
}

//五不中  生肖連  尾連
$strSQLTmp = " select a.Bet_Type, a.Number as Number, a.Ball1, a.Ball2, a.Ball3, a.Ball4, a.Ball5, SUM(a.MemStake*ROUND(b.MemExchangeRate, 4){UserRate}) as MemStake, SUM(a.MemStake*(a.Odds1 - 1)*ROUND(b.MemExchangeRate, 4){UserRate}) as MemPayOff, SUM(a.MemStake*(a.Odds2 - 1)*ROUND(b.MemExchangeRate, 4){UserRate}) as MemPayOff2, COUNT(a.ID) as WagerCount from `bet_ParlaysNumberEven` a, `bet_BetListTmp` b where a.GambleID = ".$intGID." and a.isPayOff = 0 and a.Bet_Type = {BT} and a.BetListID = b.ID".$strSeach." group by a.Number order by MemPayOff desc limit 0, 20 ";
$strSQLTmp = str_replace("{UserRate}", $strUserRate, $strSQLTmp);

$arrBT = array(30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43);
$arrSQL = array();
foreach($arrBT as $intBT){
	$fp = str_replace("{BT$intBT}", $_arrBet_Type[1][$intBT], $fp);
	if($intBT == 30)continue;
	$arrSQL[] = "(" .str_replace("{BT}", $intBT, $strSQLTmp).")";
}
$strSQL = implode(" union ", $arrSQL);

//print $strSQL;
if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errorhidden"] = "Find Data Error!";
	$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".mysql_error($$_Conn).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
while($objR = PMA_mysql_fetch_object($RS)){
	$arrRows[$objR -> Bet_Type][0][] = $objR;
}

//$arrAnimals = getAnimalsParamsByDate(1, date("Y-m-d"));
//$arrAnimals = getExChangeAnimalsParams($arrAnimals);

//條列式數字 選擇頁數
preg_match("/<!--@ BetList31 Start @\/\/-->(.+)<!--@ BetList31 End @\/\/-->/s", $fp, $arrCon);

foreach($arrBT as $intBT){
	$strCon = "";
	$strNumSite = "";
	if(is_array($arrRows[$intBT])){
		foreach($arrRows[$intBT] as $arrR){
			$strCon = "";
			$strNumSite = "";
			foreach($arrR as $objR){
				$strTmp = $arrCon[1];
				$strNum = "";
				$strNumSite = "";

				switch($intBT){
					case 30:
						$strNumSite = "_".$objR -> Number;

						$arrNum = explode(",", $objR -> JoinNumber);
						$arrN = array();
						foreach($arrNum as $strN){
							$arrN[] = $_arrAnimals[(int)$strN];
						}
						$strNum = implode(", ", $arrN);
						break;

					case 31:
						$strNum = sprintf("%02s", $objR -> Ball1).", ".sprintf("%02s", $objR -> Ball2).", ".sprintf("%02s", $objR -> Ball3).", ".sprintf("%02s", $objR -> Ball4).", ".sprintf("%02s", $objR -> Ball5);
						break;

					case 37:
					case 34:
						$strNum = $_arrAnimals[$objR -> Ball4];
					case 36:
					case 33:
						$strNum = $_arrAnimals[$objR -> Ball3].($strNum != ""?", ".$strNum:"");
					case 35:
					case 32:
						$strNum = $_arrAnimals[$objR -> Ball1].", ".$_arrAnimals[$objR -> Ball2].($strNum != ""?", ".$strNum:"");
						break;

					case 43:
					case 40:
						$strNum = $objR -> Ball4;
					case 42:
					case 39:
						$strNum = $objR -> Ball3.($strNum != ""?", ".$strNum:"");
					case 41:
					case 38:
						$strNum = $objR -> Ball1.", ".$objR -> Ball2.($strNum != ""?", ".$strNum:"");
						break;
				}

				$strTmp = str_replace("{ShowNumber}", $strNum, $strTmp);
				$strTmp = str_replace("{ShowNo}", $objR -> WagerCount, $strTmp);
				$strTmp = str_replace("{ShowStake}", round($objR -> MemStake), $strTmp);
				$strTmp = str_replace("{ShowPayOff}", round($objR -> MemPayOff), $strTmp);

				$strCon .= $strTmp;
			}
			$fp = preg_replace("/<!--@ BetList$intBT$strNumSite Start @\/\/-->.+<!--@ BetList$intBT$strNumSite End @\/\/-->/s", $strCon, $fp);
		}
	}
	$fp = preg_replace("/<!--@ BetList$intBT$strNumSite Start @\/\/-->.+<!--@ BetList$intBT$strNumSite End @\/\/-->/s", $strCon, $fp);
}
$fp = preg_replace("/<!--@ BetList30_0 Start @\/\/-->.+<!--@ BetList30_0 End @\/\/-->/s", $strCon, $fp);
$fp = preg_replace("/<!--@ BetList30_1 Start @\/\/-->.+<!--@ BetList30_1 End @\/\/-->/s", $strCon, $fp);

//輸出
WebOutputHTML($fp, 1);
?>